package cn.com.code.generator.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字符串处理
 *
 * @author machendong
 * @date 2018年11月5日 上午11:59:32
 */
public class StringUtils {

    /**
     * 将下划线命名格式转为驼峰命名格式，如：table_name => tableName, TABLE_NAME => tableName
     * @param target 待转化的字符串
     * @return
     */
    public static String mapUnderscoreToCamelCase(String target) {
        if (isEmpty(target)) return null;
        String[] arr = target.split("_");
        if (arr.length == 1) return target;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            if (i == 0) {
                sb.append(arr[i].toLowerCase());
                continue;
            }
            StringBuilder tmp = new StringBuilder(arr[i].toLowerCase());
            tmp.replace(0,1, tmp.substring(0, 1).toUpperCase());
            sb.append(tmp);
        }
        return sb.toString();
    }

    /**
     * 将map的key转为驼峰法命名
     * @param map
     * @return
     */
    public static <T> Map<String, T> mapUnderscoreToCamelCase( Map<String, T> map) {
        Map<String, T> res = new HashMap<>();
        for (Map.Entry<String, T> entry : map.entrySet()) {
            String key = StringUtils.mapUnderscoreToCamelCase(entry.getKey());
            T value = entry.getValue();
            res.put(key,value );
        }
        return res;
    }

    /**
     * 将集合中每个map的key转为驼峰法命名
     * @param list
     * @return
     */
    public static <T> List<Map<String, T>> mapUnderscoreToCamelCase(List<Map<String, T>> list) {
        List<Map<String, T>> res = new ArrayList<>();
        for (Map<String, T> m : list) {
            res.add(mapUnderscoreToCamelCase(m));
        }
        return res;
    }

    /**
     * 判断是否空串
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        if (str==null || str.equals("")) return true;
        return false;
    }

}
